include(FetchContent)

# ================== vcpkg配置，必须位于project之前
# 避免每次都运行 vcpkg install;
# 如果需要重新运行 vcpkg install, 可以设置 cmake命令行参数 -DVCPKG_MANIFEST_INSTALL=ON
option(VCPKG_MANIFEST_INSTALL "re-run vcpkg install" OFF)

# 用于支持自定义依赖版本; see https://learn.microsoft.com/en-us/vcpkg/users/versioning#version-schemes
set(VCPKG_FEATURE_FLAGS "versions")

# ------------------ details
# set(VCPKG_TARGET_TRIPLET x64-windows) # 设置 vcpkg target triplet
# set(VCPKG_CMAKE_SYSTEM_NAME Windows)
set(MYVCPKG_USE_EXPORTED TRUE)
set(MYVCPKG_DIST_USE_LOCAL) # 使用 git repo 根目录安装的 vcpkg/
set(MYVCPKG_TC_RELA_PATH "scripts/buildsystems/vcpkg.cmake") # toolchain 文件相对目录

# ------------------ vcpkg 清单模式 installed 目录
set(MYVCPKG_MANIFEST_INST_DIR "${ROOT_DIR}/vcpkg_installed") # 默认为 根目录/vcpkg_installed/

# ------------------ vcpkg export; see https://learn.microsoft.com/en-us/vcpkg/commands/export
set(MYVCPKG_EXPORTED_DIR "${ROOT_DIR}/sdk/vcpkg-export-20250809-200132")
set(MYVCPKG_EXPORTED_INST_DIR "${MYVCPKG_EXPORTED_DIR}/installed")

set(CMAKE_TOOLCHAIN_FILE "") # vcpkg 工具链文件

if(MYVCPKG_USE_EXPORTED)
    # ------------------ 使用 vcpkg export 目录
    set(VCPKG_INSTALLED_DIR ${MYVCPKG_EXPORTED_INST_DIR})
    set(CMAKE_TOOLCHAIN_FILE "${MYVCPKG_EXPORTED_DIR}/${MYVCPKG_TC_RELA_PATH}" CACHE STRING "vcpkg toolchain file")
else()
    # ----------------- 使用 manifest installed 目录
    set(VCPKG_INSTALLED_DIR "${MYVCPKG_MANIFEST_INST_DIR}")

    if(MYVCPKG_DIST_USE_LOCAL) # 使用 git repo 局部安装的 vcpkg
        if(NOT EXISTS VCPKG_ROOT)
            # ------- 克隆 vcpkg
            if(NOT EXISTS "${VCPKG_ROOT}/.git")
                find_program(GIT_EXECUTABLE git REQUIRED)
                message(STATUS "Cloning vcpkg ...")
                execute_process(
                    COMMAND "${GIT_EXECUTABLE}" clone https://github.com/microsoft/vcpkg.git
                    WORKING_DIRECTORY "${ROOT_DIR}"
                    COMMAND_ERROR_IS_FATAL ANY
                )
            endif()

            # ------- 执行 bootstrap
            if(WIN32)
                set(BOOTSTRAP_SCRIPT "${VCPKG_ROOT}/bootstrap-vcpkg.bat")
            else()
                set(BOOTSTRAP_SCRIPT "${VCPKG_ROOT}/bootstrap-vcpkg.sh")
            endif()

            # 只有发现 vcpkg 可执行文件不存在时才 bootstrap
            set(VCPKG_EXEC "${VCPKG_ROOT}/vcpkg")

            if(NOT EXISTS "${VCPKG_EXEC}${CMAKE_EXECUTABLE_SUFFIX}")
                message(STATUS "Bootstrapping vcpkg ...")
                execute_process(
                    COMMAND "${BOOTSTRAP_SCRIPT}"
                    WORKING_DIRECTORY "${VCPKG_ROOT}"
                    COMMAND_ERROR_IS_FATAL ANY
                )
            endif()

            # ------- 让当前项目使用刚准备好的 vcpkg toolchain
            set(VCPKG_ROOT "${ROOT_DIR}/vcpkg")
            set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/${MYVCPKG_TC_RELA_PATH}"
                CACHE STRING "Vcpkg toolchain file, repo local")
        endif() # --- clone local vcpkg repo
    elseif(DEFINED ENV{VCPKG_ROOT})
        set(VCPKG_ROOT "$ENV{VCPKG_ROOT}") # 使用 用户安装的 vcpkg
        set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/${MYVCPKG_TC_RELA_PATH}"
            CACHE STRING "Vcpkg toolchain file, user")
    else()
        message(WARNING "Set your environment var: VCPKG_ROOT")
    endif()
endif()